﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DentScan2.DB;
using System.Text;
using AjaxControlToolkit;

namespace DentScan2.WebApp.UC
{

    public partial class ucDDL : Classes.BaseUserControl
    {
        DLDataContext dl = new DLDataContext();

        public Enums.Classificators Classificator
        {
            get;
            set;
        }

        public bool CanInsert
        {
            get;
            set;
        }

        public bool AddNull
        {
            get;
            set;
        }

        public Guid? SelectedId
        {
            get
            {
                return ddl.SelectedValue == "" ? null : Guid.Parse(ddl.SelectedValue) as Guid?;
            }
            set
            {
                ddl.SelectedValue = value == null ? "" : value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetProperFilter(ftetb);
                Display(SelectedId);
                ibAdd.Visible = CanInsert;

                string guid = Guid.NewGuid().ToString();
                string valGroupName = "vgDDL_" + guid;
                rfvTxt.ValidationGroup = valGroupName;
                bSave.ValidationGroup = valGroupName;
            }

        }


        

        private void Display(Guid? id)
        {
            ddl.SelectedValue = null;
            Guid areaId = (this.Page as Classes.BaseForm).AreaId.Value;
            switch (Classificator)
            {

                case Enums.Classificators.CarTypes:
                    ddl.DataTextField = "CarTypeName";
                    ddl.DataValueField = "CarTypeId";
                    ddl.DataSource = dl.CarTypes.Where(item => !item.IsDel && item.AreaId == areaId).OrderBy(item => item.CarTypeName);
                    break;
                case Enums.Classificators.Colors:
                    ddl.DataTextField = "ColorName";
                    ddl.DataValueField = "ColorId";
                    ddl.DataSource = dl.Colors.Where(item => !item.IsDel && item.AreaId == areaId).OrderBy(item => item.ColorName);
                    break;
                case Enums.Classificators.Insurers:
                    ddl.DataTextField = "InsurerName";
                    ddl.DataValueField = "InsurerId";
                    ddl.DataSource = dl.Insurers.Where(item => !item.IsDel && item.AreaId == areaId).OrderBy(item => item.InsurerName);
                    break;
                case Enums.Classificators.Paints:
                    ddl.DataTextField = "PaintName";
                    ddl.DataValueField = "PaintId";
                    ddl.DataSource = dl.Paints.Where(item => !item.IsDel && item.AreaId == areaId).OrderBy(item => item.PaintName);
                    break;
                case Enums.Classificators.Users:
                    ddl.DataTextField = "FullName";
                    ddl.DataValueField = "UserId";
                    ddl.DataSource = dl.Users.Where(item => !item.IsDel).OrderBy(item => item.FullName);
                    break;
                case Enums.Classificators.Evaluators:
                    ddl.DataTextField = "EvaluatorName";
                    ddl.DataValueField = "EvaluatorId";
                    ddl.DataSource = dl.Evaluators.Where(item => !item.IsDel && item.AreaId == areaId).OrderBy(item => item.EvaluatorName);
                    break;
            }

            ddl.DataBind();
            if (AddNull)
                ddl.Items.Insert(0, new ListItem("", ""));

            if (id.HasValue)
                ddl.SelectedValue = id.Value.ToString();
            else
                if (ddl.Items.Count > 0)
                    ddl.Items[0].Selected = true;
        }

        private void Insert()
        {
            tb.Text = "";
            tb.Visible = true;
            ddl.Visible = false;
            tb.Focus();
            ibAdd.Visible = false;
            bSave.Visible = true;
            bCancel.Visible = true;
            //mv.SetActiveView(vNew);
        }

        private void Cancel()
        {
            //mv.SetActiveView(vShow);
            tb.Visible = false;
            ddl.Visible = true;
            ibAdd.Visible = true;
            bSave.Visible = false;
            bCancel.Visible = false;
        }

        private void Save()
        {
            Page.Validate("vgDDL");
            if (!Page.IsValid)
                return;

            Guid id = Guid.NewGuid();
            switch (Classificator)
            {
                case Enums.Classificators.CarTypes:
                    DB.CarType carType = new CarType()
                    {
                        CarTypeId = id,
                        CarTypeName = tb.Text.ToUpper(),
                        IsDel = false,
                        LastEditDate = DateTime.UtcNow,
                        AreaId = (this.Page as Classes.BaseForm).AreaId.Value
                    };
                    dl.CarTypes.InsertOnSubmit(carType);
                    break;
                case Enums.Classificators.Colors:
                    DB.Color color = new Color()
                    {
                        ColorId = id,
                        ColorName = tb.Text.ToUpper(),
                        IsDel = false,
                        LastEditDate = DateTime.UtcNow,
                        AreaId = (this.Page as Classes.BaseForm).AreaId.Value
                    };
                    dl.Colors.InsertOnSubmit(color);
                    break;
                case Enums.Classificators.Insurers:
                    DB.Insurer insurer = new Insurer()
                    {
                        InsurerId = id,
                        InsurerName = tb.Text.ToUpper(),
                        IsDel = false,
                        LastEditDate = DateTime.UtcNow,
                        AreaId = (this.Page as Classes.BaseForm).AreaId.Value
                    };
                    dl.Insurers.InsertOnSubmit(insurer);
                    break;
                case Enums.Classificators.Paints:
                    DB.Paint paint = new Paint()
                    {
                        PaintId = id,
                        PaintName = tb.Text.ToUpper(),
                        IsDel = false,
                        LastEditDate = DateTime.UtcNow,
                        AreaId = (this.Page as Classes.BaseForm).AreaId.Value
                    };
                    dl.Paints.InsertOnSubmit(paint);
                    break;
                case Enums.Classificators.Evaluators:
                    DB.Evaluator evaluator = new Evaluator()
                    {
                        EvaluatorId = id,
                        EvaluatorName = tb.Text.ToUpper(),
                        IsDel = false,
                        LastEditDate = DateTime.UtcNow,
                        AreaId = (this.Page as Classes.BaseForm).AreaId.Value
                    };
                    dl.Evaluators.InsertOnSubmit(evaluator);
                    break;
            }
            dl.SubmitChanges();

            Cancel();

            Display(id);
            //mv.SetActiveView(vShow);
        }

        protected void ibAdd_Click(object sender, ImageClickEventArgs e)
        {
            Insert();
        }

        protected void bSave_Click(object sender, ImageClickEventArgs e)
        {
            Save();
        }

        protected void bCancel_Click(object sender, ImageClickEventArgs e)
        {
            Cancel();
        }


    }
}